# -*- coding:utf-8 -*-
class TreeNode:
    def __init__(self, x):
        self.val = x
        self.left = None
        self.right = None
class Solution:
    def TreeDepth(self, pRoot):
        # write code here
        self.length_list = []
        self.length = 0
        self.helper(pRoot)
        return max(self.length_list)
    def helper(self, root):
        if not root:
            self.length_list.append(self.length)

            return
        self.length += 1
        self.helper(root.left)
        self.helper(root.right)
        self.length -= 1

# root = TreeNode(1)
# root.left = TreeNode(2)
# root.left.left = TreeNode(4)
# root.left.right = TreeNode(5)
# root.left.right.left = TreeNode(7)
# root.right = TreeNode(3)
# root.right.right = TreeNode(6)
# test = Solution()
# print(test.TreeDepth(root))


class Solution:
    def TreeDepth(self, pRoot):
        # write code here
        #   使用层次遍历
        #   当树为空直接返回0
        if pRoot is None:
            return 0
            # 方法2：使用递归
            #  如果该树只有一个结点，它的深度为1.如果根节点只有左子树没有右子树，
            #  那么树的深度为左子树的深度加1；同样，如果只有右子树没有左子树，
            #  那么树的深度为右子树的深度加1。如果既有左子树也有右子树，
            #  那该树的深度就是左子树和右子树的最大值加1.
        count = max(self.TreeDepth(pRoot.left), self.TreeDepth(pRoot.right)) + 1
        return count